package com.shuoyue.ycgk.views.dialog;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.shuoyue.ycgk.R;

import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.listeners.OnSingleWheelListener;
import cn.addapp.pickers.picker.SinglePicker;

/**
 * 自定义顶部及底部
 *
 * @author matt
 * blog: addapp.cn
 */
public class CustomPicker extends SinglePicker<String> implements OnSingleWheelListener {
    private TextView titleView;
    String titleStr;

    public CustomPicker(AppCompatActivity activity, String[] array, String titleStr) {
        super(activity, array);
        this.titleStr = titleStr;
        setSelectedIndex(1);
        setLineConfig(new LineConfig(0.06f));
        setOnSingleWheelListener(this);
    }

    @Override
    protected void showAfter() {
        View rootView = getRootView();
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator alpha = ObjectAnimator.ofFloat(rootView, "alpha", 0, 1);
        ObjectAnimator translation = ObjectAnimator.ofFloat(rootView, "translationY", 300, 0);
        animatorSet.playTogether(alpha, translation);
        animatorSet.setDuration(100);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.start();
    }

    @Override
    public void dismiss() {
        View rootView = getRootView();
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator alpha = ObjectAnimator.ofFloat(rootView, "alpha", 1, 0);
        ObjectAnimator translation = ObjectAnimator.ofFloat(rootView, "translationY", 0, rootView.getHeight());
//        ObjectAnimator rotation = ObjectAnimator.ofFloat(rootView, "rotation", 0, 120);
        animatorSet.playTogether(alpha, translation);
        animatorSet.setDuration(200);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                dismissImmediately();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();
    }

    @Nullable
    @Override
    protected View makeHeaderView() {
        View view = LayoutInflater.from(activity).inflate(R.layout.picker_header, null);
        titleView = view.findViewById(R.id.title);
        if (TextUtils.isEmpty(titleStr)) {
            titleView.setText(titleText);
        }
        view.findViewById(R.id.cancer).setOnClickListener(v -> dismiss());
        view.findViewById(R.id.submit).setOnClickListener(v -> {
            onSubmit();
            dismiss();
        });
        return view;
    }

    @Nullable
    @Override
    protected View makeFooterView() {
        View view = LayoutInflater.from(activity).inflate(R.layout.picker_footer, null);
        Button submitView = view.findViewById(R.id.submit);
        submitView.setText(submitText);
        submitView.setOnClickListener(v -> {
            dismiss();
            onSubmit();
        });
        return null;
    }

    @Override
    public void onWheeled(int index, String item) {
        if (titleView != null) {
            titleView.setText(item);
        }
    }

    @Override
    public void onSubmit() {
        super.onSubmit();
    }

    @Override
    protected void onCancel() {
        super.onCancel();
    }
}
